package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 体检预约管理
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    //体检预约
    public Result order(Map map) throws Exception {
        //1.检查用户所预约的日期是否已经体检进行了预约设置，如果没有则无法进行预约
        String orderDate = (String) map.get("orderDate");//预约日期
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if(orderSetting == null){
            //证明指定日期没有进行预约设置，用户无法体检预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查用户所选的日期是否已经约满，如果约满则无法预约
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if(reservations >= number){
            //已经约满
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3.检查用户是否重复预约，判断条件：同一用户在同一天预约了同一个套餐，则无法完成预约
        String telephone = (String) map.get("telephone");//用户手机号
        Member member = memberDao.findByTelephone(telephone);
        if(member != null){
            //证明根据手机号查询到了会员信息
            //去判断用户是否重复预约
            Integer id = member.getId();//会员用户id
            Date check_orderDate = DateUtils.parseString2Date(orderDate);//预约日期
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(id, check_orderDate, Integer.parseInt(setmealId));

            //根据条件进行查询
            List<Order> list = orderDao.findByCondition(order);
            if(list != null && list.size() > 0){
                //根据该3个条件进行查询仍然查询到结果，证明该用户已经进行了预约，还在重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {//4.检查用户是否是会员，如果是会员直接完成预约，如果不是将用户自动注册为会员，在进行预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());

            //完成会员的添加，自动注册
            memberDao.add(member);
        }

        //5.预约成功，更新表中当日已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员id
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//设置预约日期
        order.setOrderType((String) map.get("orderType"));//设置预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//设置预约到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//设置预约套餐id

        //保存预约
        orderDao.add(order);

        //更新当天已预约人数
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //根据id查询预约套餐信息(体检人姓名 t_member，预约日期t_order ，套餐名称t_setmeal，预约类型t_order )
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
